<html>
<head><title>Suzanna's Calculator</title></head>
<body>

<h1>Suzanna's Calculator</h1>
Type an expression in the following box (e.g., 10.5+20*3/25).
<p>
<form method="GET">
<input type="text" name="expr"><input type="submit" value="Calculate">
</form>
</p>

	<?php
		if(isset($_GET["expr"])){
			$myExpr = $_GET["expr"];
			if(preg_match("#^(-?[0-9]+\.?[0-9]*\s*[+-/*]\s*)*-?[0-9]*\.?[0-9]*$#", $myExpr)){
				//check for /0
				if(preg_match("#/\s*0+\.?0*#", $myExpr))
					$ans = "Don't divide by zero!";
				else if(preg_match("#--#", $myExpr))
					$ans = "Invalid Expression";
				else
					eval("\$ans = $myExpr;");
			}else{
				$ans = "Invalid Expression";
			}
			echo "<h2>Result</h2> " . $ans;
		}
	?>

</body>
</html>
